package org.example.util;


import java.io.*;

/**
 * @Title: 文件读取
 * @ClassName: com.ruoyi.vr.util.FileUtils.java
 * @Description:
 *
 * @Copyright 2020-2021 - Powered By 研发中心
 * @author: 王延飞
 * @date: 2020/1/24 17:28
 * @version V1.0
 */
public class FileUtils {
    /**
     * 将文本文件中的内容读入到String中
     * @param buffer buffer
     * @param filePath 文件路径
     * @throws IOException 异常
     * @date 2020-1-7
     */
    public static String readToBuffer(StringBuffer buffer, String filePath) throws IOException {
        InputStream is = new FileInputStream(filePath);
        String line; // 用来保存每行读取的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine(); // 读取第一行
        while (line != null) { // 如果 line 为空说明读完了
            buffer.append(line); // 将读到的内容添加到 buffer 中
            buffer.append("\n"); // 添加换行符
            line = reader.readLine(); // 读取下一行
        }
        reader.close();
        is.close();
        return buffer.toString();
    }

    /**
     * @Title: 将文本文件中的内容读入到String中 <JDK8自动地关闭资源>
     * @MethodName: readToBufferJDK8
     * @param buffer
     * @param filePath
     * @Return void
     * @Exception
     * @Description: https://blog.csdn.net/fly910905/article/details/86093723
     *
     * @author: 王延飞
     * @date: 2020/10/24 17:40
     */
    public static String readToBufferJDK8(StringBuffer buffer, String filePath) throws IOException {

        String line; // 用来保存每行读取的内容
        File file = new File(filePath);
        if (file.exists()) {
            try (InputStream is = new FileInputStream(filePath);
                 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            ) {
                while ((line = reader.readLine()) != null) { // 读取行,如果 line 为空说明读完了
                    buffer.append(line); // 将读到的内容添加到 buffer 中
                }
            } catch (IOException e) {
                return null;
            }
        }
        return buffer.toString();
    }

    public static void writeToFileZipFileContents(String zipFileName,
                                                  String outputFileName)
            throws java.io.IOException {

        java.nio.charset.Charset charset =
                java.nio.charset.StandardCharsets.US_ASCII;
        java.nio.file.Path outputFilePath =
                java.nio.file.Paths.get(outputFileName);

        // Open zip file and create output file with
        // try-with-resources statement

        try (
                java.util.zip.ZipFile zf =
                        new java.util.zip.ZipFile(zipFileName);
                java.io.BufferedWriter writer =
                        java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
        ) {
            // Enumerate each entry
            for (java.util.Enumeration entries =
                 zf.entries(); entries.hasMoreElements();) {
                // Get the entry name and write it to the output file
                String newLine = System.getProperty("line.separator");
                String zipEntryName =
                        ((java.util.zip.ZipEntry)entries.nextElement()).getName() +
                                newLine;
                writer.write(zipEntryName, 0, zipEntryName.length());
            }
        }
    }

    public static void main(String[] args) throws IOException {
        StringBuffer sb = new StringBuffer();
        String s = FileUtils.readToBufferJDK8(sb, "C:\\Users\\FLY\\Desktop\\线下实训\\test.html");
        System.out.println(s);
    }
}